package com.zzty.txj.controller;


import com.zzty.txj.entity.*;
import com.zzty.txj.service.DpostService;
import com.zzty.txj.service.PostService;
import com.zzty.txj.service.SportService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/dpost")
public class DpostController {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(DpostController.class);
    @Autowired
    DpostService dpostService;
    @Autowired
    PostService postService;
    @Autowired
    SportService sportService;
    String openId="";

    @ResponseBody
    @RequestMapping("/getUser")
    public Daily getUser(User user){
        logger.info("Dpost/getUser"+user);
        String result="";
        String result1="";
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        String date=String.valueOf(df.format(day));

        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        String date1=String.valueOf(df1.format(day));

        Daily daily1=new Daily();

        openId=user.getOpenId();
        Daily daily=new Daily();
        User user1=dpostService.getUser(user);

        String dailyName=postService.getName(user1.getId());

        dailyName=dailyName.replaceAll(",","");
        DoDaily doDaily=new DoDaily();
        doDaily.setDate(date1);
        doDaily.setUserId(user1.getId());

        List<DoDaily>doDailies=dpostService.getDoDaily(doDaily);

        for(int i=0;i<doDailies.size();i++){
               String name=postService.getDailyName(doDailies.get(i).getDailyId());
               daily=dpostService.getDaily(doDailies.get(i).getDailyId());
               if(date.compareTo(daily.getStartTime())>0&&date.compareTo(daily.getStopTime())<0){
                   if(1==doDailies.get(i).getResult()){
                       result="该项已打卡";
                       break;
                   }

                   daily1.setId(daily.getId());
                   result=name;
                   if(date.compareTo(dpostService.getDaily(6).getStartTime())>0&&date.compareTo(dpostService.getDaily(6).getStopTime())<0){
                       logger.info("result222"+result1);
                       for(int j=0;j<doDailies.size();j++){

                           daily1.setId(6);

                           logger.info("doDailies.get(j).getDailyId()==5&&doDailies.g"+doDailies.get(j).getDailyId()+"  "+doDailies.get(j).getResult());
                           if(doDailies.get(j).getDailyId()==5&&doDailies.get(j).getResult()==1){
                               result=name;
                               result1=name;
                               logger.info("result1"+result1);
                               break;
                           }
                           logger.info("result1111"+result1);
//                           else{
//                               result="午睡没有打卡";
//                               break;
//                           }
                       }
                       break;
                   }
                   break;
               }else{
                   result="不在当前时间段";
               }
        }
        logger.info("result1"+result1);
        if("".equals(result1)&&"午睡起床".equals(result)){
            result="午睡没有打卡";
        }
        if(""==result){
            result="该时间没有任务";
        }
        daily1.setName(result);

        return daily1;
    }



        @ResponseBody
        @RequestMapping("/getOpenId")
        public Map<String,String> getOpenId(HttpServletRequest request){
        HashMap<String,String>hashMap=new HashMap<>();
        String openId=(String)request.getSession().getAttribute("openId");
        hashMap.put("name",openId);

        return hashMap;
    }


    @ResponseBody
    @RequestMapping("/checkLocation")
    public Map<String,List<Location>> checkLocation(Integer id){
        logger.info("DpostcheckLocation"+id);
        Map<String,List<Location>>map=new HashMap<>();
            Daily daily=dpostService.getDaily1(id);
            List<Location>locations=new ArrayList<>();
            if(daily.getLocation()==null){
                map.put("result1",null);
                return map;
            }
            else{
                String location=daily.getLocation().replaceAll(",","");
                for(int i=0;i<location.length();i++){
                    char a[]=location.toCharArray();
                    String b=String.valueOf(a[i]);
                    Integer c=Integer.valueOf(b);
                    Location location1=dpostService.getLocation(c);
                    locations.add(location1);

                }
                map.put("result1",locations);
                return map;
            }
    }

    @ResponseBody
    @RequestMapping("/update")
    public Integer updateDodaily (Integer id){
            Date day=new Date();
            SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
            String date1=String.valueOf(df1.format(day));
            User user=new User();
            user.setOpenId(openId);
            User user1=dpostService.getUser(user);

            DoDaily doDaily=new DoDaily();
            doDaily.setUserId(user1.getId());
            doDaily.setDailyId(id);
            doDaily.setDate(date1);
            Integer count=dpostService.getCount(doDaily);
            return count;
    }

    @ResponseBody
    @RequestMapping("/getUserId")
    public Integer getUserId(HttpServletRequest request){
        User user=new User();
        String openId1=(String)request.getSession().getAttribute("openId");

        user.setOpenId(openId1);
        User user1=dpostService.getUser(user);
        return user1.getId();
    }


    @ResponseBody
    @RequestMapping("/getLocation")
    public List<Location>getLocation(){
        Daily daily=dpostService.getDaily1(8);
        List<Location>locations=new ArrayList<>();
        String location=daily.getLocation().replaceAll(",","");
        for(int i=0;i<location.length();i++){
            char a[]=location.toCharArray();
            String b=String.valueOf(a[i]);
            Integer c=Integer.valueOf(b);
            Location location1=dpostService.getLocation(c);
            locations.add(location1);

        }
        return locations;
    }
}
